package cn.lcl.web.servlet;

import cn.lcl.bean.PageBean;
import cn.lcl.bean.po.Student;
import cn.lcl.factory.ServiceFactory;
import cn.lcl.service.BaseService;
import cn.lcl.service.IAdminService;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by licho on 2017/8/16 0016.
 */
@WebServlet("/admin/*")
public class AdminServlet extends BaseServlet {

    private IAdminService adminService= ServiceFactory.getService(IAdminService.class);//获得服务层

    private static  Logger log= Logger.getLogger(AdminServlet.class);

    /**
     * 默认的处理方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PageBean<Student> pageBean=adminService.showStuLists();
        log.info("PageB ean:"+pageBean);
        req.setAttribute("page",pageBean);
        req.getRequestDispatcher("/WEB-INF/jsp/admin/stulist.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                doGet(req,resp);
    }

    /**
     *可以通过/index映射到该方法
     * @param req
     * @param response
     * @return
     */
    public String index(HttpServletRequest req, HttpServletResponse response)  {
        response.setHeader("Content-type","text/plain;charset=UTF-8");
        log.info("你调用成功啦，哈哈哈哈");
        try {
            response.getWriter().write("恭喜你成功了");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String login(HttpServletRequest req,HttpServletResponse resp){
        req.setAttribute("name","李崇林");
        return "index";
    }

    /**
     * 直接跳转
     * @param req
     * @param resp
     * @return
     */
    public String direct(HttpServletRequest req,HttpServletResponse resp){
        return REDIRECT+"200.jsp";
    }
}
